package mws;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;


// 单个线程池的配置属性
@Data
public class ThreadPoolProperty {

    public final static String NOMAL = "nomal";
    public final static String SCHEDULE = "schedule";
    public final static AtomicInteger DEFAULT_PREFIX = new AtomicInteger(0);

    // JSON序列化时的字段名为"name"
    @JsonProperty("name")
    private String name;

    // 线程池类型（normal/schedule）
    @JsonProperty("type")
    private String type;

    // 核心线程数
    @JsonProperty("corePoolSize")
    private int corePoolSize = 1;

    // 最大线程数
    @JsonProperty("maxPoolSize")
    private int maxPoolSize = 1;

    // 队列容量
    @JsonProperty("queueCapacity")
    private int queueCapacity = -1;

    // 空闲线程存活时间（秒）
    @JsonProperty("keepAliveSeconds")
    private int keepAliveSeconds;

    // 线程名称前缀
    @JsonProperty("threadNamePrefix")
    private String threadNamePrefix = "common_pool" + DEFAULT_PREFIX.incrementAndGet();

    // 拒绝策略
    @JsonProperty("rejectedExecutionHandler")
    private String rejectedExecutionHandler = "default-no-policy";

    // 是否允许核心线程超时
    @JsonProperty("allowCoreThreadTimeout")
    private boolean allowCoreThreadTimeout;

    // 是否启用该线程池
    @JsonProperty("enabled")
    private boolean enabled;

    // 定时任务特有配置（仅schedule类型有效）
    @JsonProperty("scheduledConfig")
    private ScheduledConfig scheduledConfig;

    @Data
    public static class ScheduledConfig {
        // 取消任务时是否从队列中移除
        @JsonProperty("removeOnCancelPolicy")
        private boolean removeOnCancelPolicy;

        // 关闭后是否继续执行现有周期性任务
        @JsonProperty("continueExistingPeriodicTasksAfterShutdownPolicy")
        private boolean continueExistingPeriodicTasksAfterShutdownPolicy;
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ThreadPoolProperty that = (ThreadPoolProperty) o;
        return corePoolSize == that.corePoolSize &&
                maxPoolSize == that.maxPoolSize &&
                queueCapacity == that.queueCapacity &&
                keepAliveSeconds == that.keepAliveSeconds &&
                allowCoreThreadTimeout == that.allowCoreThreadTimeout &&
                enabled == that.enabled &&
                Objects.equals(name, that.name) &&
                Objects.equals(type, that.type) &&
                Objects.equals(threadNamePrefix, that.threadNamePrefix) &&
                Objects.equals(rejectedExecutionHandler, that.rejectedExecutionHandler) &&
                Objects.equals(scheduledConfig, that.scheduledConfig);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, type, corePoolSize, maxPoolSize, queueCapacity,
                keepAliveSeconds, threadNamePrefix, rejectedExecutionHandler,
                allowCoreThreadTimeout, enabled, scheduledConfig);
    }

    /**
     * 创建默认线程池配置
     * 适用于大多数常规任务场景
     */
    public static ThreadPoolProperty createDefault() {
        ThreadPoolProperty defaultConfig = new ThreadPoolProperty();

        // 基础配置
        defaultConfig.setName("defaultThreadPool");
        defaultConfig.setType(NOMAL);
        defaultConfig.setEnabled(true);

        // 线程池大小：默认核心线程数为CPU核心数，最大为核心数*2
        int coreCount = Runtime.getRuntime().availableProcessors();
        defaultConfig.setCorePoolSize(coreCount);
        defaultConfig.setMaxPoolSize(coreCount * 2);

        // 队列容量：默认1000
        defaultConfig.setQueueCapacity(1000);

        // 空闲线程存活时间：默认60秒
        defaultConfig.setKeepAliveSeconds(60);

        // 线程名前缀：便于日志追踪
        defaultConfig.setThreadNamePrefix("default-thread-");

        // 默认拒绝策略：AbortPolicy（抛出异常）
        defaultConfig.setRejectedExecutionHandler("AbortPolicy");

        // 不允许核心线程超时
        defaultConfig.setAllowCoreThreadTimeout(false);

        // 定时任务默认配置
        ScheduledConfig scheduledConfig = new ScheduledConfig();
        scheduledConfig.setRemoveOnCancelPolicy(false);
        scheduledConfig.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
        defaultConfig.setScheduledConfig(scheduledConfig);

        return defaultConfig;
    }
}